// pages/my/myorder/myorder.js
var publicFun = require('../../../utils/publicFun.js');
var wxPay = require('../../../utils/wxPay.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shipping_name: '',
    shipping_no: '',
    ship_time: '',
    order_type: 10,
    isLookingLogistics: false,
    orderList: [],
    logistics: [],
    recommendsGoods: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var order_type = options.order_type || 10;
    //计算弹出窗口高度
    var pageHeight = app.globalData.pageHeight;
    var popHeight = (pageHeight - 260) / app.globalData.pixelRatio;
    //初始化页面顶部的bar
    this.initBarOptions(order_type);
    //初始化scrolllist
    this.initScrollList(order_type);
    this.setData({
      order_type: order_type,
      scrollListHeight: app.globalData.pageHeight - 110,
      popHeight: popHeight
    })
    if (!this.data.orderList.length) {
      this.getHotData();
    }
  },

  initScrollList: function (order_type) {
    var orderOptions = {
      list_id: 'orderList',
      url: '/api/index.php?app=order&act=lists',
      data: {
        status: order_type
      }
    }
    this.setData({
      orderOptions: orderOptions
    })
  },

  initBarOptions: function (order_type) {
    var selectedIdx;
    if (order_type == 10) {
      selectedIdx = 0;
    } else if (order_type == 20) {
      selectedIdx = 1;
    } else if (order_type == 30) {
      selectedIdx = 2;
    } else {
      selectedIdx = 3;
    }
    //初始化头部栏
    var options = {
      item: {
        width: 187.5,
        height: 88,
        size: 30,
        color: '#999999',
        selectedColor: '#1f1f1f',
        selectedIdx: selectedIdx,
      },
      slider: {
        width: 90,
        color: '#1f1f1f',
      },
      data: [{
        name: '待付款',
        id: 0,
        type: 10,
      }, {
        name: '待发货',
        id: 1,
        type: 20,
      }, {
        name: '待收货',
        id: 2,
        type: 30,
      }, {
        name: '已完成',
        id: 3,
        type: 40,
      }]
    };
    this.setData({
      navbarOptions: options
    })
  },

  //再次购买商品
  buyAgain: function (e) {
    let targetId = e.currentTarget.dataset.idx;
    wx.navigateTo({
      url: '../../goods_detail/goods_detail?goods_id=' + targetId,
    })
  },

  //确认收货
  confirmOrder: function (e) {
    let targetId = e.currentTarget.dataset.idx;
    // console.log(targetId);
    let self = this;
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success: function (res) {
        if (res.confirm) {
          app.Ajax({
            method: 'POST',
            data: { order_id: targetId },
            url: '/api/index.php?app=order&act=take_delivery',
            success: (res) => {
              self.toastShow('收货成功');
              self.initScrollList(self.data.order_type);
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },

  //取消订单
  cancelOrder: function (e) {
    let targetId = e.currentTarget.dataset.idx;
    // console.log(targetId);
    let self = this;
    wx.showModal({
      title: '提示',
      content: '是否确定取消订单',
      success: function (res) {
        if (res.confirm) {
          app.Ajax({
            method: 'POST',
            data: { order_id: targetId },
            url: '/api/index.php?app=order&act=cancel',
            success: (res) => {
              self.initScrollList(self.data.order_type);
              self.toastShow('取消成功');
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },

  //获取热门推荐的商品
  getHotData: function () {
    app.Ajax({
      url: '/api/index.php?app=category&act=group_goods',
      data: {
        group_id: 1,
        per_page: 10,
      },
      success: (res) => {
        // console.log(res.list);
        this.setData({
          recommendsGoods: res.list
        })
      },
      fail: (err) => {
       
      }
    })
  },

  //查看热门商品
  lookGoodsInfo: function (e) {
    wx.navigateTo({
      url: '../../goods_detail/goods_detail?group_id=1&goods_id=' + e.detail.id,
    })
  },

  //滚动加载更多的数据
  loadMoreData: function (e) {
    if (e.detail.data.length > 0) {
      for (let item of e.detail.data) {
        item['goodsinfo'] = {
          quantity: item.goods[0].quantity,
          goods_attr: item.goods[0].sku_attr,
          goods_id: item.goods[0].goods_id,
          goods_image: item.goods[0].goods_image,
          goods_name: item.goods[0].goods_name,
          sell_price: item.goods[0].sell_price,
          goods_attr_arr: [item.goods[0].sku_attr]
        }
        item['reducePrice'] = publicFun.accMul(item.stars, 0.01, 2);
      }
    }
    // console.log(e.detail.data);
    this.setData({
      orderList: e.detail.data,
    })
  },

  /**
   * 头部切换
   */
  navbarClick: function (e) {
    //设置当前的订单类型
    this.setData({
      order_type: e.detail.type
    });
    this.initScrollList(e.detail.type);
  },

  /**
   * 查看订单详情
   */
  lookOrderInfo: function (e) {
    var target = e.currentTarget.dataset.target;
    // console.log(target);
    var targetId
    if (target.status == 10) {
      targetId = target.order_id;
      wx.navigateTo({
        url: '../../order/goods_order_confirm/goods_order_confirm?order_id=' + targetId
      })
    } else {
      targetId = target.order_id;
      wx.navigateTo({
        url: './orderInfo/orderInfo?order_id=' + targetId,
      })
    }
  },

  /**
   * 付款
   */
  payOrder: function (e) {
    let targetId = e.currentTarget.dataset.idx;
    wxPay.wxPayMoneyByOrderid(targetId, (succ) => {
      wx.redirectTo({
        url: '../my/myorder/myorder?order_type=1',
      })
    }, (err, code) => {
      err = err || '支付失败'
      this.toastShow(err);
    })
  },

  /**
   * 查看物流信息
   */
  logisticsInfoOpen: function (e) {
    let targetId = e.currentTarget.dataset.idx;
    // console.log(targetId);
    //获取物流单号等信息
    app.Ajax({
      url: '/api/index.php?app=order&act=detail',
      data: {
        order_id: targetId
      },
      success: (res) => {
        // console.log(res);
        this.setData({
          shipping_name: res.shipping_name,
          ship_time: res.ship_time,
          shipping_no: res.shipping_no,
          isLookingLogistics: true,
        })
        if (res.express) {
          this.setData({
            logistics: res.express.data,
          })
        }
      },
      fail: (err) => {
        console.log(err);
      }
    })
  },

  /**
   * 关闭物流信息
   */
  logisticsInfoClose: function (e) {
    this.setData({
      isLookingLogistics: false,
    })
  },
  /**
  * 显示报错信息
  */
  toastShow: function (message) {
    this.setData({
      message: message,
    })
  },
})